<?php
require(dirname(__FILE__) . '/includes/init1.php');
require(dirname(__FILE__) . '/includes/simple_html_dom.php');
set_time_limit(0);
/*$starturl = "http://www.yihaodian.com/ctg/searchPage/c25887-%E8%91%A1%E8%90%84%E9%85%92/b920325/a-s1-v0-p{1,1}-price-d0-f04-m1-rt0-pid-k/";
$urllists = array();
if(preg_match("~\{(\d+),(\d+)\}~",$starturl,$pagenum)){ 
	$pagebegin = intval($pagenum[1]); 
	$pageend = intval($pagenum[2]);
	for(;$pagebegin<=$pageend;$pagebegin++){ 
		$url = str_replace($pagenum[0],$pagebegin,$starturl);
		array_push($urllists,$url); 
	} 
}
$products_list = array();
foreach($urllists as $index=>$url){
	$curl = curl_init(); 
	curl_setopt($curl, CURLOPT_URL,$url); 
	curl_setopt($curl, CURLOPT_REFERER, 'http://www.yihaodian.com'); 
	curl_setopt($curl,CURLOPT_COOKIE,'__utma=148959678.929533370.1345632607.1348216623.1348468501.32; __utmz=148959678.1347931358.27.12.utmcsr=youhi.cn|utmccn=(referral)|utmcmd=referral|utmcct=/crm/yihaodian_d.php; provinceId=1; grouponAreaId=22; abtest=87; search_keyword_history=%E6%B3%95%E5%9B%BD%E7%BA%A2%E9%85%92%2C%E4%BD%B3%E6%A2%A6%2Casd%2C%E6%82%A6%E5%9B%BD%2C%E7%BA%A2%E9%85%92; search_browse_history=4260600%2C2823693%2C965555%2C22164%2C1669288%2C2811501%2C4353367%2C27990%2C1176826%2C1137154; cart_cookie__f0f89e67-aca1-4d56-a3cf-31a5b79f7e41_getAllProductNum=1; cart_cookie__f0f89e67-aca1-4d56-a3cf-31a5b79f7e41="1,2349325x1x1"; msessionid=ARKRMWPNAMNY881GGYBM6B6GR7MUHSBT; NSC_wjq-xfc-80=ffffffff09091e5845525d5f4f58455e445a4a4229a0; __utmb=148959678.5.10.1348468501; __utmc=148959678; tracker_msessionid=B87XR67W16RJDD8VTZQV7PSW5EMBY69U; JSESSIONID=824F67C429D875E791E8EC8139E329FD; cart_cookie_uuid=f0f89e67-aca1-4d56-a3cf-31a5b79f7e41');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
	$result = curl_exec($curl); 
	curl_close($curl);
	$yhd_re = (array)json_decode($result);
	$str = $yhd_re['value'];
	$html = str_get_html($str);
	foreach($html->find('.title') as $index=>$h){
		$sku = get_str($h->href,'http://www.yihaodian.com/product/','_');
		$name = $h->title;
		//array_push($sku_list,$sku);
		$products_list[$index]['sku'] = $sku;
		$products_list[$index]['name'] = $name;
	}
}
$html->clear();
unset($html);
foreach($products_list as $products){*/
	$pl_url = 'http://www.yihaodian.com/product/ajaxViewExperience.do';
	$curlPost1 = 'product.id=1137154&merchantId=784&orderType=newest&pagenationVO.currentPage={1,15}&pagenationVO.rownumperpage=5';
	$curl_lists = array();
	if(preg_match("~\{(\d+),(\d+)\}~",$curlPost1,$pagenum)){ 
		$pagebegin = intval($pagenum[1]); 
		$pageend = intval($pagenum[2]);
		for(;$pagebegin<=$pageend;$pagebegin++){ 
			$url = str_replace($pagenum[0],$pagebegin,$curlPost1);
			array_push($curl_lists,$url); 
		} 
	}
	foreach($curl_lists as $curlPost){
    
		$pinglun_list = array();
		//echo $curlPost.'<br>';
		$curl = curl_init(); 
		curl_setopt($curl, CURLOPT_URL,$pl_url); 
		curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
		curl_setopt($curl, CURLOPT_REFERER, 'http://www.yihaodian.com'); 
		curl_setopt($curl,CURLOPT_COOKIE,'cookie_for_delivery_destination=1%2C476%2C38%2C%2C%25E7%2594%25B11%25E5%258F%25B7%25E5%25BA%2597%25E9%2585%258D%25E9%2580%2581%25EF%25BC%258C%25E9%25A2%2584%25E8%25AE%25A1%25E5%2595%2586%25E5%2593%2581%25E5%2587%25BA%25E5%25BA%2593%25E5%2590%258E1~3%25E5%25A4%25A9%25E5%2586%2585%25E9%2580%2581%25E8%25BE%25BE%2C1%2C1%2C3%2C0%2C; __utma=148959678.929533370.1345632607.1348216623.1348468501.32; __utmz=148959678.1347931358.27.12.utmcsr=youhi.cn|utmccn=(referral)|utmcmd=referral|utmcct=/crm/yihaodian_d.php; provinceId=1; grouponAreaId=22; abtest=87; search_keyword_history=%E6%B3%95%E5%9B%BD%E7%BA%A2%E9%85%92%2C%E4%BD%B3%E6%A2%A6%2Casd%2C%E6%82%A6%E5%9B%BD%2C%E7%BA%A2%E9%85%92; search_browse_history=4260600%2C2823693%2C965555%2C22164%2C1669288%2C2811501%2C4353367%2C27990%2C1176826%2C1137154; cart_cookie__f0f89e67-aca1-4d56-a3cf-31a5b79f7e41_getAllProductNum=1; cart_cookie__f0f89e67-aca1-4d56-a3cf-31a5b79f7e41="1,2349325x1x1"; msessionid=ARKRMWPNAMNY881GGYBM6B6GR7MUHSBT; NSC_wjq-xfc-80=ffffffff09091e5845525d5f4f58455e445a4a4229a0; __utmb=148959678.5.10.1348468501; __utmc=148959678; tracker_msessionid=B87XR67W16RJDD8VTZQV7PSW5EMBY69U; JSESSIONID=824F67C429D875E791E8EC8139E329FD; cart_cookie_uuid=f0f89e67-aca1-4d56-a3cf-31a5b79f7e41');
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		$result = curl_exec($curl); 
		curl_close($curl);
		$html = str_get_html($result);
		if($html){
			$xx='';
			foreach($html->find('.time') as $j=>$h){
				$time = $h->innertext;
				//$pinglun_list[$j]['time'] = $time; 
				$xx = $time.',';
			}
			//file_put_contents('D:/time.txt',$xx);
			$tr=fopen("D:/time.txt","a+");//此处用a+，读写方式打开，将文件指针指向文件末尾。如果文件不存在则尝试创建之
			fwrite($tr,$xx);
			fclose($tr);
			$yy='';
			foreach($html->find('.user_other dl dd',0) as $k=>$e){
				$pinglun = '';
				if(strpos($e->innertext,'<strong class="merit">食用心得 ：</strong>')){
					$pinglun = get_str($e->innertext,'<dd><strong class="merit">食用心得 ：</strong>','</dd>');
				}else if(strpos($e->innertext,'<strong class="merit">购物心得 ：</strong>')){
					$pinglun = get_str($e->innertext,'<dd><strong class="merit">购物心得 ：</strong>','</dd>');
				}
				//$pinglun_list[$k]['pinglun'] = $pinglun; 
				//echo $pinglun='$comment$'.$pinglun.',';
				//array_push($pinglun_list,$pinglun);
				if($pinglun){
					$yy = '$comment$'.$pinglun.',';
				}
			}
			//file_put_contents('D:/msg.txt',$yy);
			$td=fopen("D:/msg.txt","a+");//此处用a+，读写方式打开，将文件指针指向文件末尾。如果文件不存在则尝试创建之
			fwrite($td,$yy);
			fclose($td);
			//echo $yy;
			/*foreach($pinglun_list as $com){
				if(!empty($com['pinglun'])){
					$db->query("insert into ".$ecs->table('pinglun')." (products_sn,products_name,time,comments) values ('".$products['sku']."','".$products['name']."','".$com['time']."','".$com['pinglun']."')");
				}
			}*/
			$html->clear();
			unset($html);
		}
	}

function get_str($str,$start_str,$end_str){
   $start_pos = strpos($str,$start_str)+strlen($start_str);  
   $end_pos = strpos($str,$end_str);  
   $c_str_l = $end_pos - $start_pos;  
   $contents = substr($str,$start_pos,$c_str_l);  
   return $contents;  
} 
?>